<?php

/**
 * 根据已经存在的优惠券，创建白名单
 * @author lyl
 **/


ini_set('display_errors', 1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Doraemon\model\Phone as mPhone;
use Biz\module\Classes\CouponRebate as cCouponRebate;
use Biz\model\salesRebate\SalesCoupon as mSalesCoupon;
use Biz\model\activity\CouponConfig as mCouponConfig;
use Biz\service\activity\Coupon as sCoupon;

$data = [
    '1101'=>[
        'phone'=> '13524190505',
        'type'=> 1,
        'coupon'=>'TPD337RUY',
    ],
    '1219'=>[
        'phone'=> '13918071410',
        'type'=> 1,
        'coupon'=>'TPD44XNET',
    ],
    '1220'=>[
        'phone'=> '13661010592',
        'type'=> 1,
        'coupon'=>'TPD4544JY',
    ],
    '1221'=>[
        'phone'=> '15821214433',
        'type'=> 1,
        'coupon'=>'TPD46XNKM',
    ],
    '1287'=>[
        'phone'=> '18611592928',
        'type'=> 1,
        'coupon'=>'TPD51C3MN',
    ],
    '1289'=>[
        'phone'=> '18678851750',
        'type'=> 1,
        'coupon'=>'TPD533NEV',
    ],
    '1303'=>[
        'phone'=> '18683750925',
        'type'=> 2,
        'coupon'=>'TPD673FWM',
    ],
    '1298'=>[
        'phone'=> '13916734124',
        'type'=> 1,
        'coupon'=>'TPD62UXVA',
    ],
];

foreach($data as $_k=>&$_v){
    $result[$_k] = [
        'uid'=> mPhone::getUidByPhone($_v['phone']),
        'status'=> 1,
    ];
    if($_v['type'] == 1){
        $result[$_k]['person_coupon_id'] = $_k;
        $result[$_k]['person_coupon_code'] = $_v['coupon'];
    }else if($_v['type'] == 2){
        $result[$_k]['company_coupon_id'] = $_k;
        $result[$_k]['company_coupon_code'] = $_v['coupon'];
    }
}

$params = array_column($result, NULL,'uid');

$getCoupon = mSalesCoupon::select('uid', ['uid'=>array_keys($params)]);
if($getCoupon){
    $params = array_filter($params, function($_data)use($getCoupon){
        if(in_array($_data['uid'], $getCoupon)){
            unset($_data);
        }
    });
}

if(!empty($params)){
    foreach($params as $_k=>&$_v){
        if($_v['uid']) {
            $tcoupon = cCouponRebate::genRebateCouponCode($_v['uid']);

            if (isset($_v['person_coupon_id'])) {
                $tpCoupon = [
                    'code' => $_v['person_coupon_code'],
                    'id' => $_v['person_coupon_id']
                ];
                $couponList = mCouponConfig::createRebateCoupon([$tcoupon['tc']]);
                $tcCoupon = [
                    'code' => $tcoupon['tc'],
                    'id' => $couponList[$tcoupon['tc']],
                ];
            } else if (isset($_v['company_coupon_id'])) {
                $tcCoupon = [
                    'code' => $_v['company_coupon_code'],
                    'id' => $_v['company_coupon_id']
                ];
                $couponList = mCouponConfig::createRebateCoupon([$tcoupon['tp']]);
                $tpCoupon = [
                    'code' => $tcoupon['tp'],
                    'id' => $couponList[$tcoupon['tp']],
                ];
            }

            if (!empty($couponList)) {
                foreach ($couponList as $id) {
                    sCoupon::cacheCouponConfig($id);
                }
            }

            $ret = mSalesCoupon::createOne(
                $_v['uid'],
                $tcCoupon,
                $tpCoupon
            );
            if (!$ret) {
                echo '<=======创建返佣码出错：' . json_encode($_v, true) . '=======>'.PHP_EOL;
            }else{
                echo '<=======创建返佣码成功：' . json_encode($ret, true) . '=======>'.PHP_EOL;
            }

            unset($tcoupon, $tcCoupon, $tpCoupon, $couponList);
        }else{
            echo '<=======uid不存在：' . json_encode($_v, true) . '=======>'.PHP_EOL;
        }
    }
}







